/*
 * Clock.c
 *
 * Created: 14.01.2013 14:31:42
 *  Author: saenko
 */ 

#include <avr/io.h>
#include "../inc/spi.h"
#include "../inc/twi.h"
#include "../inc/max7219.h"
#include "../inc/ds3232.h"

#define MAIN_MODE 0
#define SET_HOUR_MODE 1
#define SET_MIN_MODE 2

volatile unsigned char ds3232_int;
volatile unsigned char button_int;

volatile unsigned char hours;
volatile unsigned char minutes;

volatile unsigned char key_pressed;

volatile unsigned char status;

volatile unsigned char half_sec;

volatile unsigned char blink_hour;

volatile unsigned blink_min;

ISR(INT0_vect)
{
	
	unsigned char temp = PIND & (1<<PD2);
	if(temp>0) ds3232_int = 1;
	else half_sec = 1;
	
}

ISR(PCINT1_vect)
{
	button_int = 1;
	key_pressed = PINC & 0b00001111;
}

int main(void)
{
	DDRD = 0b10000000;
	PORTD = (1<<PD2);
	
	DDRC = 0b00000000;
	PORTC = 0b00001111;
	
	PCICR = (1<<PCIE1);
	PCMSK1 = 0b00001111;
	
	spi_init();
	twi_init(0b01111111);

	
    max7219_cmd(0x09,0x0F);	
	max7219_cmd(0x0B,0x04);	
    max7219_cmd(0x0C,0x01);
	
	/*  max7219_cmd(0x01,0x01);
	    max7219_cmd(0x02,0x02);
		max7219_cmd(0x03,0x03);
		max7219_cmd(0x04,0x04);	*/
   //	max7219_test();	
	
	
	
	sei();
	
	
	
	hours = 0x09;
	minutes = 0x37;
	
    ad3232_set_time(hours,minutes);
	
	ad3232_init();	
	
//	ad3232_get_time();
	
	half_sec= 0;
	
    while(1)
    {
		if(ds3232_int)
		{
			ds3232_int = 0;
			ad3232_get_time();
		}
		
		if(twi_flags.receive_end)
		{
			twi_flags.receive_end = 0;
			if(status != MAIN_MODE)
			{
				hours = twi_buf_rx[1];
				minutes = twi_buf_rx[0];				
			}

			max7219_out_time(hours,minutes);			
		}
		
		if(half_sec)
		{
			half_sec= 0;
			if(blink_hour) max7219_out_time(0xFF,minutes);
			if(blink_min) max7219_out_time(hours,0xFF);
		}
		
		switch(key_pressed)
		{
			case 0b00000111:
				switch(status)
				{
				case MAIN_MODE: 
					status = SET_HOUR_MODE;
					blink_hour = 1;
					blink_min = 0;
				break;
				case SET_HOUR_MODE:
					status = SET_MIN_MODE;
					blink_hour = 0;
					blink_min = 1;
				break;
				case SET_MIN_MODE:
					status = MAIN_MODE;
					blink_hour = 0;
					blink_min = 0;
					ad3232_set_time(hours,minutes);
				break;
				}
				break;
				
		/*	case 0b00001011:
				switch(status)
				{
				case SET_HOUR_MODE:
					hours++;
					max7219_out_time(hours,minutes);
				break;
					
				case SET_MIN_MODE:
					minutes++;
					max7219_out_time(hours,minutes);
				break;	
				}
			*/
		}
		

			
    }
}